package GUILayer;
import ControlLayer.CtrEmployee;
import ModelLayer.Employee;
import javax.swing.table.DefaultTableModel;
import java.util.ArrayList;

public class GUIEmployee extends javax.swing.JFrame {
    private CtrEmployee ctrEmployee;

    public GUIEmployee() {
        initComponents();
        ctrEmployee = CtrEmployee.getInstance();
        loadTableContent();
    }

    private void loadTableContent() {
        ((DefaultTableModel)tblEmployeeList.getModel()).setRowCount(0);
        ArrayList<Employee> employeeList = ctrEmployee.getEmployeeList();
        for (Employee employee : employeeList) {
            ((DefaultTableModel)tblEmployeeList.getModel()).addRow(
                new Object [] {
                    employee.getIDEmployee(),
                    employee.getIDProfile(),
                    employee.getLastname(),
                    employee.getFirstname(),
                    employee.getEmail(),
                    employee.getPasswd(),
                    employee.getActive()
                }
            );
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrlEmployeeList = new javax.swing.JScrollPane();
        tblEmployeeList = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tblEmployeeList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "id", "id_profile", "lastname", "firstname", "email", "passwd", "active"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        scrlEmployeeList.setViewportView(tblEmployeeList);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrlEmployeeList, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrlEmployeeList, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUIEmployee().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane scrlEmployeeList;
    private javax.swing.JTable tblEmployeeList;
    // End of variables declaration//GEN-END:variables
}
